package com.meixiang.adapter.moments;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.lzy.okhttputils.model.HttpParams;
import com.meixiang.R;
import com.meixiang.activity.homes.service.ShowPictureActivity;
import com.meixiang.activity.moments.ReplyDetailsActivity;
import com.meixiang.dialog.CustomPopup;
import com.meixiang.entity.moments.PostDetailsItemBean;
import com.meixiang.entity.moments.PostDetailsList;
import com.meixiang.global.Config;
import com.meixiang.http.HttpCallBack;
import com.meixiang.http.HttpUtils;
import com.meixiang.main.MXApplication;
import com.meixiang.recyclerview.RecyclerAdapter;
import com.meixiang.tool.Tool;
import com.meixiang.util.GlideHelper;
import com.meixiang.view.flowlayout.CustomFlowAdapter;
import com.meixiang.view.flowlayout.CustomFlowLayout;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 作者：黑哥 on 2016/5/7 14:46
 */
public class PostDetailsAdapter extends RecyclerAdapter<RecyclerView.ViewHolder> {
    private static final int TYPE_HEADER = 0, TYPE_ITEM = 1, TYPE_FOOT = 2;
    private boolean isAddHead;
    private boolean isAddFoot;
    private int footViewSize = 1, headViewSize = 1;
    public PostDetailsList mBean;
    private List<PostDetailsItemBean> mItemBean = new ArrayList<>();
    private Context context;
    private CustomPopup popupWindow;
    private Activity mActivity;
    private WindowManager.LayoutParams lp;

    private int mPosition;
    public PostDetailsList getData(){
        return mBean;
    }

    public List<PostDetailsItemBean>  getListData(){
        return mItemBean;
    }


    public void addAll(List<PostDetailsItemBean> mItemBean) {
        this.mItemBean = mItemBean;
        this.notifyDataSetChanged();
    }

    public void clear() {
        this.mItemBean.clear();
        this.notifyDataSetChanged();
    }

    public void addData(PostDetailsList mBean) {
        this.mBean = mBean;

    }

    public PostDetailsAdapter(Context context, Activity mActivity) {
        this.context = context;
        this.mActivity = mActivity;
    }

    public int getContentItemCount() {

        return mItemBean.size();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        if (viewType == TYPE_HEADER) {
            View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_postdetails, parent, false);
            HeaderViewHolder viewHolder = new HeaderViewHolder(itemView);
            return viewHolder;
        }else if (viewType == TYPE_ITEM) {
            View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_reply, parent, false);
            ItemViewHolder viewHolder = new ItemViewHolder(itemView);
            return viewHolder;
        }else if (viewType == TYPE_FOOT) {
            View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.foot_item, parent, false);
            FootViewHolder viewHolder = new FootViewHolder(itemView);
            return viewHolder;
        }
        return null;

    }

    @SuppressWarnings("unchecked")
    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof ItemViewHolder) {
            if (mListener != null) {
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Log.e("TAG","查看详情position="+position);
                        if(mItemBean.size() < 3){
                            mPosition=position-1;
                        }
                        mListener.onItemClick(holder.itemView, mPosition);

                    }
                });
            }



            if (mItemBean != null && mItemBean.size() > 0) {
                GlideHelper.showCircleImage(context, mItemBean.get(position - 1).getMemberAvatar(), R.mipmap.default_head_portrait, ((ItemViewHolder) holder).mIvUserHeadPortrait);
                ((ItemViewHolder) holder).mTvUserName.setText(mItemBean.get(position - 1).getMemberName());
                ((ItemViewHolder) holder).mTvTime.setText(mItemBean.get(position - 1).getCommentTime());
                ((ItemViewHolder) holder).mTvContent.setText(mItemBean.get(position - 1).getComment());
                if (mItemBean.get(position - 1).getAnswerName() == null) {
                    ((ItemViewHolder) holder).mTvOtherUserName.setVisibility(View.GONE);
                    ((ItemViewHolder) holder).mTvUserReply.setVisibility(View.GONE);
                } else {

                    ((ItemViewHolder) holder).mTvOtherUserName.setText(mItemBean.get(position - 1).getAnswerName());
                }
                ((ItemViewHolder) holder).mTvTime.setText(mItemBean.get(position - 1).getCreateTime());
            }

        } else if (holder instanceof HeaderViewHolder) {

            ((HeaderViewHolder) holder).mTvAttention.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mListener != null) {
                        mListener.onAttentionItemClick(v, position);
                    }
                }
            });

            ((HeaderViewHolder) holder).mCustomFlowLayout.setOnFlowItemClickListener(new CustomFlowLayout.OnFlowItemClickListener() {
                @Override
                public void onItemClick(int position, View view) {
                    if (Tool.isFastDoubleClick()) return;
                    List myList = new ArrayList();
                    Collections.addAll(myList,mBean.getCommentImages());
                    Intent intent = new Intent(context, ShowPictureActivity.class);
                    intent.putStringArrayListExtra("urls", (ArrayList<String>) myList);
                    intent.putExtra("position", position);
                    context.startActivity(intent);
                }
            });
            //设置数据
            if (mBean != null) {

                //设置用户图像
                GlideHelper.showCircleImage(context, mBean.getMemberAvatar(), R.mipmap.default_head_portrait, ((HeaderViewHolder) holder).mIvUserPortrait);
                ((HeaderViewHolder) holder).mTvUserName.setText(mBean.getMemberName());
                ((HeaderViewHolder) holder).mTvContent.setText(mBean.getComment());
                ((HeaderViewHolder) holder).mCustomFlowLayout.setMargin(0, 5, 5, 0);
                final String[] urls = mBean.getCommentImages();
                if (urls == null || urls.length == 0) {
                    ((HeaderViewHolder) holder).mCustomFlowLayout.setVisibility(View.GONE);
                } else {
                    ((HeaderViewHolder) holder).mCustomFlowLayout.setVisibility(View.VISIBLE);
                    ((HeaderViewHolder) holder).mCustomFlowLayout.setAdapter(new CustomFlowAdapter(urls) {
                        @Override
                        public View getView(CustomFlowLayout parent, int position, Object o) {
                            View layout = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_attention_image, parent, false);
                            ImageView imageView = (ImageView) layout.findViewById(R.id.iv_image);
                            GlideHelper.showImage(context, (String) o, R.drawable.image_default_gray_bg, imageView);
                            return layout;
                        }
                    });

                }
                ((HeaderViewHolder) holder).mTvPostTime.setText(mBean.getCreateTime());
                if (mBean.getMemberId().equals(MXApplication.mApp.getUser().getMemberId())) {
                    ((HeaderViewHolder) holder).mTvAttention.setVisibility(View.GONE);
                } else {
                    ((HeaderViewHolder) holder).mTvAttention.setVisibility(View.VISIBLE);
                    if (mBean.getMyConcern().equals("1")) {
                        ((HeaderViewHolder) holder).mTvAttention.setText(R.string.moments_has_attention);
                    } else {
                        ((HeaderViewHolder) holder).mTvAttention.setText(R.string.moments_attention);
                    }
                    ((HeaderViewHolder) holder).mTvAttention.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            //未关注
                            if (mBean.getMyConcern().equals("0")) {
                                AddConcern(mBean, position);
                            } else {
                                CancelConcern(mBean, position);
                            }
                        }
                    });
                }

            }

        } else if (holder instanceof FootViewHolder) {
            if (mItemBean.size() < 3) {
                holder.itemView.setVisibility(View.GONE);
                return;
            } else {
                holder.itemView.setVisibility(View.VISIBLE);
                ((FootViewHolder) holder).mTvCheckMoreComment.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(context, ReplyDetailsActivity.class);
                        intent.putExtra("commentId", mBean.getCommentId());
                        context.startActivity(intent);
                    }
                });

                ((FootViewHolder) holder).mTvCheckMoreComment.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(context, ReplyDetailsActivity.class);
                        intent.putExtra("commentId", mBean.getCommentId());
                        context.startActivity(intent);
                    }
                });
            }

        }


    }

    @Override
    public int getItemCount() {
        return mItemBean.size() + headViewSize + footViewSize;
    }

//    private OnItemClickListener mOnItemClickListener;
//
//    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
//        this.mOnItemClickListener = mOnItemClickListener;
//    }
//
//    public interface OnItemClickListener {
//        void onItemClick(View view, int position);
//    }
    private onItemClickListener mListener = null;

    public void setOnItemClickListener(onItemClickListener listener) {
        mListener = listener;
    }

    public interface onItemClickListener {

        /**

         * 点赞点击事件

         * @param v

         * @param position

         */
        void onItemClick(View v, int position);


        /**

         *  关注点击事件

         * @param v

         * @param position

         */
        void onAttentionItemClick(View v,int position);
        /**

         *  图片点击事件

         * @param v

         * @param position

         */

        void onPhotoonItemClick(View v,int position);

    }
    @Override
    public int getItemViewType(int position) {
        int type = TYPE_ITEM;
        if (headViewSize == 1 && position == 0) {
            type = TYPE_HEADER;
        } else if (footViewSize == 1 && position == getItemCount() - 1) {
            type = TYPE_FOOT;
        }
        return type;


    }
    //关注
    private void AddConcern(final PostDetailsList mBean,final  int position){
        HttpParams params = new HttpParams();
        params.put("commentId", mBean.getCommentId());
        HttpUtils.post(Config.MOMENTS_COMMUNITY_ADDCONCERN, params, new HttpCallBack(mActivity) {
            @Override
            public void onSucceed(JSONObject result, String code, String message) {
                Tool.showTextToast(mActivity, message);
                mBean.setMyConcern("1");
                PostDetailsAdapter.this.notifyItemChanged(0);
            }

            @Override
            public void onError(String code, String message) {
                Tool.showTextToast(mActivity, message);
            }
        });
    }
    //取消关注
    private void CancelConcern(final PostDetailsList mBean,final  int position) {

        HttpParams params = new HttpParams();
        params.put("commentId", mBean.getCommentId());
        HttpUtils.post(Config.MOMENTS_COMMUNITY_CANCELCONCERN, params, new HttpCallBack(mActivity) {
            @Override
            public void onSucceed(JSONObject result, String code, String message) {
                Tool.showTextToast(mActivity, message);
                mBean.setMyConcern("0");
                PostDetailsAdapter.this.notifyItemChanged(position);
            }

            @Override
            public void onError(String code, String message) {
                Tool.showTextToast(mActivity, message);
            }
        });


    }
    class ItemViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.iv_user_head_portrait)
        ImageView mIvUserHeadPortrait;
        @Bind(R.id.tv_user_name)
        TextView mTvUserName;
        @Bind(R.id.tv_user_reply)
        TextView mTvUserReply;
        @Bind(R.id.tv_other_user_name)
        TextView mTvOtherUserName;
        @Bind(R.id.tv_time)
        TextView mTvTime;
        @Bind(R.id.tv_date)
        TextView mTvDate;
        @Bind(R.id.tv_content)
        TextView mTvContent;

        public ItemViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    class HeaderViewHolder extends RecyclerView.ViewHolder {
        //用户头像
        @Bind(R.id.iv_user_portrait)
        ImageView mIvUserPortrait;
        //关注
        @Bind(R.id.tv_attention)
        TextView mTvAttention ;
        //用户姓名
        @Bind(R.id.tv_user_name)
        TextView mTvUserName;
        //帖子内容
        @Bind(R.id.tv_content)
        TextView mTvContent;
        //时间
        @Bind(R.id.tv_post_time)
        TextView mTvPostTime;
        @Bind(R.id.layout_nine_grid)
        CustomFlowLayout mCustomFlowLayout;

        public HeaderViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    class FootViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.tv_check_more_comment)
        TextView mTvCheckMoreComment;

        public FootViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

    }

}
